package com.lzy.crm.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.util.BootStrapReturn;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.domain.Control;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.ControlService;
import com.lzy.crm.util.BootJsonResponse;
import com.lzy.crm.util.UserConstants;


/**
 * 控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class ControlController {
	
	Logger logger= Logger.getLogger(ControlController.class);
	
	@Resource
	private ControlService controlService;
	
	/**
	 * 查询列表
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/control_list")  
	public String control_list(HttpServletRequest request,ModelMap model) throws Exception{
		User user = (User)request.getSession().getAttribute("userSession");
		if(StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
			return "mobile/control/control_list";
		}
		return null;
	}
	
	
	@RequestMapping(path="/control_list_data",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String control_list_data(HttpServletRequest request, @RequestParam Map<String, String> form,
			ModelMap model) {
		BootStrapReturn<Control> result = new BootStrapReturn<Control>();
		try {
			String gameId = request.getParameter("gameId");
			String kindId = request.getParameter("kindId");
			User user = (User)request.getSession().getAttribute("userSession");
			if(!StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
				return null;
			}
			Integer currentPage = Integer.parseInt(request.getParameter("numPerPage"));
			Integer numPerPage = Integer.parseInt(request.getParameter("pageNum"));
			PageQuery pageQuery = new PageQuery(numPerPage,currentPage);
			pageQuery.setIsQueryTotal(true); 
			List<Control> controlList =controlService.getControlList(gameId, kindId, pageQuery);
			result.setPage(pageQuery.getPageIndex());
			result.setTotal(pageQuery.getTotalCount());
			result.setRows(controlList);
		} catch (Exception e) {
			logger.error("控制列表查询", e);
		}
		return JsonUtil.toJson(result) ;
	}
	
	
	@RequestMapping("/control_add")  
	public String control_add(HttpServletRequest request,ModelMap model) throws Exception{
		User user = (User)request.getSession().getAttribute("userSession");
		if(StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
			return "mobile/control/control_add";
		}
		return null;
	}
	
	
	@RequestMapping(path="/control_add_save",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String doEditUser(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			String gameId=request.getParameter("gameId");
			String kindId=request.getParameter("kindId");
			String freCount=request.getParameter("freCount");
			String remark=request.getParameter("remark");
			User user = (User)request.getSession().getAttribute("userSession");
			Control controlOld = controlService.getControl(gameId, kindId);
			if(controlOld!=null){
				bootJsonResponse.setMessage("该玩家已添加到控制器,请修改或删除再重新添加。");
				bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
				return JsonUtil.toJson(bootJsonResponse);
			}
			Control control = new Control();
			control.setCreateTime(new Date());
			control.setCreateUserId(user.getId());
			control.setGameId(Integer.valueOf(gameId));
			control.setKindId(Integer.valueOf(kindId));
			control.setFreCount(Integer.parseInt(freCount));
			control.setRemark(remark);
			controlService.insertSelective(control);
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改控制器信息", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	
	
	@RequestMapping(path="/control_del",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String control_del(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			String id=request.getParameter("id");
			controlService.deleteByPrimaryKey(id);
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("删除控制器信息", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	
	@RequestMapping("/control_edit")  
	public String control_edit(HttpServletRequest request,ModelMap model) throws Exception{
		
		return "mobile/control/control_edit";
	}

}
